package org.zhb.sql.query.sys.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zhb.sql.query.core.constant.RetCode;
import org.zhb.sql.query.core.util.JsonReturn;
import org.zhb.sql.query.model.Function;
import org.zhb.sql.query.model.Role;
import org.zhb.sql.query.sys.service.FunctionService;
import org.zhb.sql.query.sys.service.RoleService;

/**
 * @ClassName: Rolecontroller。
 * @Description: Role管理Controller
 * @author: zhb
 *
 */
@RestController
@RequestMapping("/role/")
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	FunctionService functionService;
	Logger log = LoggerFactory.getLogger(RoleController.class);

	@RequestMapping("roleInfo")
	public ModelAndView roleInfo() {
		return new ModelAndView("shiro/role/roleInfo");
	}

	@RequestMapping("roleList")
	public JsonReturn roleList() {
		JsonReturn jsonReturn = new JsonReturn();
		Page<Role> page = roleService.selectModelByCondition(new PageRequest(0, 20));
		log.info("用户查询成功!!!");
		jsonReturn.setRetcode(RetCode.OK_CODE);
		jsonReturn.setRetmsg(page);
		return jsonReturn;
	}

	@RequestMapping("shareFunction")
	public ModelAndView shareFunction() {
		// 左侧查询所有角色,右侧所有权限
		List<Role> roleList = roleService.findAll(new Role());
		List<Function> functionList = functionService.findAll(new Function());
		return new ModelAndView("shiro/user/shareFunction").addObject("roles", roleList).addObject("functions",
				functionList);
	}

	@RequestMapping("allocationFunction")
	public JsonReturn allocationFunction() {
		JsonReturn jsonReturn = new JsonReturn();
		return jsonReturn;
	}

	@RequestMapping("insert")
	public JsonReturn insert(Role role) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			roleService.save(role);
			log.info("保存成功!!!");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("添加成功");
		} catch (Exception e) {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("添加失败");
			log.info("保存成功!!!" + e.getMessage());
		}
		return jsonReturn;
	}

	@RequestMapping("selectRoleById")
	public JsonReturn selectRoleById(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			Role role = roleService.findById(id);
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonReturn;
	}

	@RequestMapping("edit")
	public JsonReturn edit(Role role) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			roleService.update(role);
			log.info("更新成功!!!");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("更新成功");
		} catch (Exception e) {
			log.info("更新失败!!!" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("更新失败");
		}
		return jsonReturn;
	}

	@RequestMapping("delete")
	public JsonReturn delete(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			roleService.delete(id);
			log.info("删除成功");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("删除成功!");
		} catch (Exception e) {
			log.info("删除失败" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("删除失败!");
			e.printStackTrace();
		}
		return jsonReturn;
	}
}